#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=500, maxa=1000000000;

class SegmentTree {
public:
  SegmentTree *left, *right;
  int l, r, v;
  
  SegmentTree(int tl, int tr) {
    left=right=NULL;
    v = 0, l=tl, r=tr;
  }

  void add(int t, int tv) {
    if (l==r) {
      v+=tv;
    } else {
      int mid=(l+r)/2;
      if (t<=mid) {
	if (!left) {
	  left = new SegmentTree(l, mid);
	}
	left->add(t, tv);
      } else {
	if (!right) {
	  right = new SegmentTree(mid+1, r);
	}
	right->add(t, tv);
      }
      v = (left ? left->v : 0)+(right ? right->v : 0);
    }
  }

  int getKth(int k) {
    if (l==r) {
      return l;
    } else {
      if (left && left->v>=k) {
	return left->getKth(k);
      } else {
	return right->getKth(k-(left ? left->v : 0));
      }
    }
  }
};

int main() {
#ifdef DEBUG
  freopen("0.in", "r", stdin);
  freopen("0.out", "w", stdout);
#endif

  static int mat[maxn+1][maxn+1];
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=n; j++) {
      scanf("%d", &mat[i][j]);
    }
  }

  for (int i=1; i<=q; i++) {
    int x1, y1, x2, y2, k;
    scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
    SegmentTree *sgt = new SegmentTree(0, maxa);
    for (int j=x1; j<=x2; j++) {
      for (int k=y1; k<=y2; k++) {
	sgt->add(mat[j][k], 1);
      }
    }
    printf("%d\n", sgt->getKth(k));
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
